package com.sz.bi

import au.com.console.jpaspecificationdsl.`in`
import au.com.console.jpaspecificationdsl.equal
import com.sz.bi.entity.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.math.BigDecimal


/**
 * 计算报表的实际数金额
 */

@Service
class TMbgActualDataService {

    @Autowired
    private lateinit var tMbgBgActualRepo: TMbgBgActualRepo

    @Autowired
    private lateinit var tMbgBgDailyAcentryRepo: TMbgBgDailyAcentryRepo

    @Autowired
    private lateinit var tMbgBgActualRecordRepo: TMbgBgActualRecordRepo

    fun getActualData(): List<ActualInfo> {
        val recordList = tMbgBgActualRecordRepo.findAll()
        val recordListByCompany = recordList.groupBy { it.fcompanyid }

        val actualList = ArrayList<ActualInfo>()
        recordListByCompany.forEach { record ->
            val actualListByCompany = ArrayList<ActualInfo>()
            val companyId = record.key
            val formIdList = record.value.map { it.fbgformid }
            formIdList.forEach {
                val acentryList = tMbgBgDailyAcentryRepo.findAll(TMbgBgDailyAcentry::fformid.equal(it)).toList()
                val dataIdList = acentryList.map { it.fdataid!! }
                val tMbgBgActualList = tMbgBgActualRepo.findAll(TMbgBgActual::fid.`in`(dataIdList))
                tMbgBgActualList.forEach {
                    actualListByCompany.add(ActualInfo(companyId, it.fformula, BigDecimal(it.fvalue!!)))
                }
            }
            val companyMap = actualListByCompany.groupBy { it.bgItemId }
            companyMap.forEach { map ->
                val formula = map.key
                val valueInfo = map.value.asSequence().map { it.value }.reduce { acc, bigDecimal -> acc!!.add(bigDecimal) }
                actualList.add(ActualInfo(companyId, formula, valueInfo))
            }
        }
        return actualList
    }

    fun getType(){
        mapOf(
                "Acc:6602" to "管理费用",
                "Acc:6602.01" to "职工薪酬",
                "Acc:6602.01.01" to "工资",
                "Acc:6602.01.02" to "职工福利费",
                "Acc:6602.01.02.01" to "福利费及其他",
                "Acc:6602.01.03" to "社会保险费",
                "Acc:6602.01.03.01" to "养老保险",
                "Acc:6602.01.03.02" to "医疗保险",
                "Acc:6602.01.03.03" to "失业保险",
                "Acc:6602.01.03.04" to "工伤保险",
                "Acc:6602.01.03.05" to "生育保险",
                "Acc:6602.01.03.06" to "大额医疗保险",
                "Acc:6602.01.03.07" to "住院保险",
                "Acc:6602.01.03.08" to "女工特种病保险",
                "Acc:6602.01.03.09" to "意外保险",
                "Acc:6602.01.03.10" to "互助医疗",
                "Acc:6602.01.04" to "住房公积金",
                "Acc:6602.01.05" to "奖金",
                "Acc:6602.01.06" to "工会经费",
                "Acc:6602.01.07" to "职工教育经费",
                "Acc:6602.01.08" to "非货币性福利",
                "Acc:6602.01.09" to "其他",
                "Acc:6602.02" to "劳动保护费",
                "Acc:6602.03" to "办公费",
                "Acc:6602.03.01" to "书报资料费",
                "Acc:6602.03.02" to "办公用品费",
                "Acc:6602.03.03" to "邮电费",
                "Acc:6602.03.04" to "印刷费",
                "Acc:6602.03.05" to "电话费",
                "Acc:6602.03.06" to "网络通讯费",
                "Acc:6602.03.07" to "材料费",
                "Acc:6602.03.99" to "其他",
                "Acc:6602.04" to "业务招待费",
                "Acc:6602.05" to "会议费",
                "Acc:6602.06" to "差旅费",
                "Acc:6602.07" to "水电费",
                "Acc:6602.07.01" to "水费",
                "Acc:6602.07.02" to "桶装水费",
                "Acc:6602.07.03" to "电费",
                "Acc:6602.08" to "物业费",
                "Acc:6602.08.01" to "房屋租赁费",
                "Acc:6602.08.02" to "房屋维修费",
                "Acc:6602.08.03" to "物业管理费",
                "Acc:6602.08.04" to "供暖费",
                "Acc:6602.08.05" to "土地租赁费",
                "Acc:6602.09" to "宣传费",
                "Acc:6602.09.01" to "CI企业形象识别",
                "Acc:6602.09.02" to "内部刊物制作费",
                "Acc:6602.09.03" to "广告费",
                "Acc:6602.09.04" to "展览费",
                "Acc:6602.09.99" to "其他宣传费",
                "Acc:6602.10" to "保险费",
                "Acc:6602.10.01" to "财产保险费",
                "Acc:6602.10.02" to "人身保险费",
                "Acc:6602.10.99" to "其他保险费",
                "Acc:6602.11" to "折旧费",
                "Acc:6602.12" to "汽车费用",
                "Acc:6602.12.01" to "车辆维修费",
                "Acc:6602.12.02" to "车辆保险费",
                "Acc:6602.12.03" to "燃油费",
                "Acc:6602.12.04" to "过路过桥费",
                "Acc:6602.12.05" to "年检费",
                "Acc:6602.12.06" to "车位费",
                "Acc:6602.12.07" to "压缩天然气",
                "Acc:6602.12.08" to "汽车租赁费",
                "Acc:6602.12.99" to "其他费用",
                "Acc:6602.14" to "修理费",
                "Acc:6602.14.01" to "房屋建筑物维修费",
                "Acc:6602.14.02" to "机械设备修理费",
                "Acc:6602.14.03" to "维保费",
                "Acc:6602.14.99" to "其他修理费",
                "Acc:6602.15" to "费用性税金",
                "Acc:6602.15.01" to "契税",
                "Acc:6602.15.02" to "残疾人保障金",
                "Acc:6602.15.99" to "其他税费",
                "Acc:6602.16" to "资产摊销",
                "Acc:6602.16.01" to "无形资产摊销",
                "Acc:6602.16.02" to "长期待摊费用摊销",
                "Acc:6602.16.03" to "低值易耗品摊销",
                "Acc:6602.16.03.01" to "库存及其他",
                "Acc:6602.16.04" to "投资性房地产摊销",
                "Acc:6602.16.99" to "筹建费用",
                "Acc:6602.17" to "中介机构费",
                "Acc:6602.17.01" to "审计费",
                "Acc:6602.17.02" to "咨询认证费",
                "Acc:6602.17.99" to "顾问费",
                "Acc:6602.18" to "诉讼仲裁费",
                "Acc:6602.19" to "拆迁村后续费用",
                "Acc:6602.19.01" to "超转人员生活费、医疗费",
                "Acc:6602.19.02" to "退2万人员费用",
                "Acc:6602.19.03" to "回迁小区物业费、维修费",
                "Acc:6602.19.04" to "原马连店商业街电费",
                "Acc:6602.19.05" to "供暖补贴",
                "Acc:6602.19.99" to "其他",
                "Acc:6602.20" to "园区公共区域费用",
                "Acc:6602.21" to "物料消耗",
                "Acc:6602.22" to "安全生产",
                "Acc:6602.23" to "安全保卫",
                "Acc:6602.24" to "累计摊销",
                "Acc:6602.25" to "保洁费",
                "Acc:6602.26" to "退休费用",
                "Acc:6602.27" to "慰问、捐赠",
                "Acc:6602.28" to "水资源费",
                "Acc:6602.29" to "污水处理费",
                "Acc:6602.30" to "碳排放交易费",
                "Acc:6602.31" to "垃圾清运费",
                "Acc:6602.32" to "协会会费",
                "Acc:6602.33" to "培训费",
                "Acc:6602.34" to "劳务费",
                "Acc:6602.34.01" to "保安及保洁",
                "Acc:6602.35" to "党建活动经费",
                "Acc:6602.35.01" to "活动经费",
                "Acc:6602.36" to "劳务派遣管理费",
                "Acc:6602.99" to "其他"
        )
    }
}

